package com.fance.highlight_springmvc4.interceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author fance
 * @date 18-5-6 下午3:53
 */
public class DemoInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        long st = System.currentTimeMillis();
        request.setAttribute("st", st);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        long st = (long) request.getAttribute("st");

        request.removeAttribute("st");
        long end = System.currentTimeMillis();
        System.out.println("处理时间为: " + (end - st) + "ms");

        request.setAttribute("handlingtime", end - st );
    }
}
